/**
FilePermisionServiceImpl.java
netfile
2012-9-10下午6:13:59
@author 屈甲康
@QQ285799123
mailto:qujiakang@126.com
 **/
package org.jackysoft.service.impl;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.jackysoft.data.SortInfo;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.ReceivedFileItem;
import org.jackysoft.entity.User;
import org.jackysoft.service.AbstractStringIdServiceImpl;
import org.jackysoft.service.FilePermissionService;
import org.jackysoft.util.SpringSecurityUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FilePermissionServiceImpl extends
		AbstractStringIdServiceImpl<ReceivedFileItem> implements
		FilePermissionService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4461410240137913073L;

	public FilePermissionServiceImpl() {
		super(ReceivedFileItem.class);
	}

	@Override
	public void passFiles(Collection<String> fileIds)
			throws DataAccessException {
		
		for (String fileId : fileIds) {
			ReceivedFileItem file = (ReceivedFileItem) getSession().get(
					ReceivedFileItem.class, fileId);
			if (file == null)
				return;
			User user = SpringSecurityUtils.getCurrentUser();
			file.setEnabled(true);
			file.setPassor(user);
		    file.setPassedDate(Calendar.getInstance());
			getSession().flush();
		}
	}

	@Override
	protected int listDataSize(SortInfo sortInfo, Map<String, Object> smap) {
		User user = SpringSecurityUtils.getCurrentUser();
		Department dept = user.getDepartment();
		String hql = String
				.format("select count(*) from %s r where r.enabled=false and r.fromer.department.topParent.id='%s'",
						entitytable(), dept.getId());
		Object o = query(hql).uniqueResult();
		return o == null ? 0 : Integer.parseInt(o.toString());
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected List<ReceivedFileItem> listData(int start, int size,
			boolean listAll, SortInfo sortInfo, Map<String, Object> smap) {
		User user = SpringSecurityUtils.getCurrentUser();
		Department dept = user.getDepartment();
		String hql = String
				.format("select new ReceivedFileItem(r.id,r.fromer,r.title,r.owner,r.createTime,r.fileType) from %s r where r.enabled=false and r.fromer.department.topParent.id='%s'",
						entitytable(), dept.getId());
		List list = query(hql).setFirstResult(start).setMaxResults(size).list();
		return list;
	}

	@Override
	protected List<ReceivedFileItem> searchData(int start, int size,
			boolean listAll, SortInfo sortInfo, Map<String, Object> smap) {
		User user = SpringSecurityUtils.getCurrentUser();
		String hql = String
				.format("select new ReceivedFileItem(r.id,r.fromer,r.title,r.owner,r.createTime,r.fileType,r.passor,r.passedDate) " +
						"from %s r where r.enabled=true and r.passor.username='%s'",
						entitytable(), user.getUsername());
		List list = query(hql).setFirstResult(start).setMaxResults(size).list();
		return list;
	}

	@Override
	protected int searchDataSize(SortInfo sortInfo, Map<String, Object> smap) {
		User user = SpringSecurityUtils.getCurrentUser();
		String hql = String
				.format("select count(*) from %s r where r.enabled=true and r.passor.username='%s'",
						entitytable(), user.getUsername());
		Object o = query(hql).uniqueResult();
		return o == null ? 0 : Integer.parseInt(o.toString());
	}
	
	

}
